package com.kcht.sylyg08.service.impl;

import com.kcht.adp.common.config.ConfigurationValue;
import com.kcht.adp.common.log.Logger;
import com.kcht.sylyg08.dao.IRFIDReportDao;
import com.kcht.sylyg08.dao.IWomsTransitionRecordDao;
import com.kcht.sylyg08.service.IReportToWOMS;
import com.kcht.sylyg08.service.model.Res;
import com.kcth.sylyg08.entity.RFIDReport;
import com.kcth.sylyg08.entity.WomsTransitionRecord;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Response;
import java.util.Objects;
import java.util.UUID;

/**
  *
  * @Package:        com.kcht.sylyg08.service.impl
  * @Description:     java类作用描述
  * @Author:         lenovo
  * @CreateDate:     2019/5/15 14:21
 */
@Stateless
@Local
public class RFIDReportToWOMS implements IReportToWOMS {
    @Inject
    private Logger logger;
    @Inject
    @ConfigurationValue
    private String WOMSURL;

    @EJB
    private IWomsTransitionRecordDao womsTransitionRecordDao;
    @EJB
    private IRFIDReportDao reportDao;

    @EJB
    private IRFIDReportDao rfidReportDao;

    @Override
    public Boolean report(String requestUuid, Boolean saved) {
        RFIDReport report = rfidReportDao.selectOneBySQL("select * from t_lzwl_rfidreport where requestUUID='" + requestUuid + "'", RFIDReport.class);
        return report(report, saved);
    }

    @Override
    public Boolean report(RFIDReport report, Boolean saved) {
        Boolean result = false;
        Client client = ClientBuilder.newClient();
        Response response = null;
        WomsTransitionRecord womsTransitionRecord = new WomsTransitionRecord();
        Res res = null;
        try {
            response = client.target(WOMSURL).request().post(Entity.entity(report, "application/json"));
            logger.info("上报料框" + report.getContainer() + report.getContainerAction() + "到woms系统");
            logger.info("response status" + response.getStatus());
            if (response.getStatus() == 200) {
                res = response.readEntity(Res.class);
                logger.info("上报成功");
                result = true;
            } else {
                throw new Exception(response.getStatusInfo().toString());
            }
        } catch (Exception e) {
//            e.printStackTrace();
            logger.error("与WOMS通信错误!" + e.getMessage());
            if (!saved) {
                String sql = "select * from t_lzwl_womstransitionrecord where requestuuid = '" + report.getRequestUUID() + "'";
                WomsTransitionRecord record = womsTransitionRecordDao.selectOneBySQL(sql, WomsTransitionRecord.class);
                if (Objects.isNull(record)) {
                    womsTransitionRecord.setUuid(UUID.randomUUID().toString().replaceAll("-", ""));
                    womsTransitionRecord.setRequestUuid(report.getRequestUUID());
                    womsTransitionRecord.setType("updateState");
                    womsTransitionRecord.setDescription(e.getMessage());
                    womsTransitionRecord.setIsModify(1);
                    womsTransitionRecordDao.save(womsTransitionRecord);
                }
            }
        } finally {
            if (Objects.nonNull(response)) {
                response.close();
            }
            if (Objects.isNull(reportDao.selectOneBySQL("select * from t_lzwl_rfidreport where requestUUID='" + report.getRequestUUID() + "'", RFIDReport.class))) {
                reportDao.save(report);
            }
            client.close();
        }
        return result;
    }

    @Override
    public Boolean report() {
        //TODO 向woms系统上报人工修改料框状态

        return null;
    }
}
